package com.example.service.Impl;

import com.example.entity.PageBean;
import com.example.entity.Schedule;
import com.example.entity.Task;
import com.example.entity.vo.*;
import com.example.mapper.ScheduleMapper;
import com.example.service.ScheduleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ScheduleMapper scheduleMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, String title, Integer id) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);

        //执行查询
        List<ScheduleTask> scheduleTaskList = new ArrayList<>();

        List<Schedule> empList = scheduleMapper.pageList(page, pageSize, title, id);

        List<Task> taskList;
        for (Schedule schedule : empList) {
            ScheduleTask scheduleTask = new ScheduleTask();
            taskList = scheduleMapper.findTaskByScheduleId(schedule.getId());
            scheduleTask.setSchedule(schedule);
            scheduleTask.setTaskList(taskList);
            scheduleTaskList.add(scheduleTask);
        }

        Page<ScheduleTask> p = (Page<ScheduleTask>) scheduleTaskList;

        //计算总页数
        Integer pageCount = (int)p.getTotal() % pageSize == 0 ? (int)p.getTotal() / pageSize : (int)p.getTotal() / pageSize + 1;

        //封装pageBean对象
        PageBean pageBean = new PageBean((int)p.getTotal(), p.getResult(), pageCount);
        return pageBean;
    }

    //学习进度展示
    @Override
    public List<Schedule> getSchedulesByUserId(Integer userId) {
        return scheduleMapper.selectByUserId(userId);
    }

    //学习任务展示
    @Override
    public List<Task> getTasksByScheduleId(Integer scheduleId) {
        return scheduleMapper.selectByScheduleId(scheduleId);
    }
    @Override
    public Integer addSchedule(String title, Integer userId) {
        if(scheduleMapper.isExistSchedule(title,userId) == null){
            LocalDateTime localDateTime = LocalDateTime.now();
            scheduleMapper.addSchedule(title,userId,localDateTime);
        } else {
            return -1;
        }
        return scheduleMapper.findScheduleById(userId,title);
    }

    @Override
    public Boolean checkSchedule(Integer userId, String title) {
        return scheduleMapper.isExistSchedule(title, userId) != null;
    }

    @Override
    public void deleteSchedule(Integer id, String title) {
        scheduleMapper.deleteSchedule(id,title);
    }

    @Override
    public void addTasks(TaskResponse taskResponse) {
        for (Node node : taskResponse.getNodeList()) {
            Task task = new Task();
            task.setScheduleId(taskResponse.getScheduleId());
            task.setDescription(node.getDescription());
            task.setResourceId(node.getId());
            task.setTitle(node.getName());
            task.setFinished(0);
            task.setImage(node.getImage());
            scheduleMapper.addTasks(task);
        }
    }

    @Override
    public Progress getTotalSchedule(Integer id) {
        Integer totalSchedule = scheduleMapper.getTotalSchedule(id);
        List<Integer> scheduleIds = scheduleMapper.getSchedule(id);
        System.out.println(scheduleIds);
        Integer totalTask = 0;
        Integer finishTask = 0;
        for (Integer scheduleId : scheduleIds) {
            totalTask += scheduleMapper.getCountTask(scheduleId);
            finishTask += scheduleMapper.getNoFinishTask(scheduleId);
        }
        Float totalProgress = scheduleMapper.getTotalProgress(id);
        return new Progress(totalProgress, totalTask, totalSchedule, finishTask);
    }

    @Override
    public List<ScheduleTask> scheduleDisplay(Integer id) {
        List<Schedule> scheduleList = scheduleMapper.selectByUserId(id);
        List<ScheduleTask> scheduleDisplayList = new ArrayList<>();
        for (Schedule schedule : scheduleList) {
            List<Task> taskList = scheduleMapper.selectByScheduleId(schedule.getId());
            scheduleDisplayList.add(new ScheduleTask(schedule, taskList));
        }
        return scheduleDisplayList;
    }

    @Override
    public void updateFinish(Integer taskId) {
        scheduleMapper.updateFinish(taskId);
        Integer id = scheduleMapper.getScheduleIdByTaskId(taskId);
        Float progress = scheduleMapper.calculateProgress(id);
        scheduleMapper.updateProgress(progress,id);
    }
}
